package fons.navigator.ui.configurations;

import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JComponent;
import javax.swing.JLabel;

import com.google.common.collect.Lists;

import fons.navigator.util.settings.Configuration;
import fons.util.Pair;

public abstract class AbstractConfigurationComponentFactory <T> implements
		ConfigurationComponentFactory {
	private Configuration configuration;	
	private String settingName;
	private final Class<T> clazz;
	
	public AbstractConfigurationComponentFactory (Class<T> clazz) {
		this.clazz = clazz;
	}
	
	
	
	@Override
	public void use(Configuration configuration, String settingName) {
		this.configuration = configuration;
		this.settingName = settingName;

	}
	
	protected String getName() {
		return settingName;
	}
	
	protected T getValue() {
		return configuration.getValueOf(configuration.getIndexOf(settingName), clazz);
	}
	
	protected void setValue(T value) {
		configuration.setValue(configuration.getIndexOf(settingName), value);
	}
	
	protected String getDescription() {
		return configuration.getDescriptionOf(configuration.getIndexOf(settingName));
	}
	
	protected Configuration getConfiguration() {
		if (configuration == null) throw new IllegalStateException();
		return configuration;
	}

	@Override
	public JComponent getLabel() {
		return new JLabel(settingName);
	}

	@Override
	public String getInfo() {
		return configuration.getDescriptionOf(configuration.getIndexOf(settingName));
	}
	
	@Override
	public Class<?> handles() {
		return this.clazz;
	}
	
	@Override
	public Collection<Pair<JComponent, ActionListener>> getAdditionalComponents() {
		return Lists.newArrayList();
	}
}
